<?php

namespace App\Http\Middleware;

use Closure;

use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;

class EmailMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $rs = $next($request);
        //注册后发邮件
        if($request->user()){
            $mail = new Message;
            //注意: 下行的setFrom要和你的邮箱名保持一致
            $mail->setFrom('aaa <18748342258@163.com>')->addTo($request->user()->email)->setSubject('试试我的中间件')->setBody('好用不!!!');

            $mailer = new smtpMailer(array(
                'host'=>'smtp.163.com',
                'username'=>'18748342258',//你的163用户名
                'password'=>'PHWlove810506'//你的邮箱密码
                ));
            $mailer->send($mail);
        }

        return $rs;
    }
}
